<?php

class VotacionController extends Controller {

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        if (!Yii::app()->getUser()->hasState('_document')) {
            $this->redirect('index.php?r=site/votacion');
        }
        $document = Yii::app()->user->_document;
        $Criteria = new CDbCriteria();
        $Criteria->condition = "Cedula = $document";
        $idProfile = Yii::app()->user->_idProfile;
        $controllers = Yii::app()->controller->getId();
        $actionsProfile = Consult::model()->getActionsProfile($idProfile, $controllers);
        $actionStatus = new JAction();

        try {
            $actionAjax = $actionStatus->getActions(ucfirst($controllers) . 'Controller', '');
        } catch (Exception $ex) {
            echo $ex->getTraceAsString();
        }
        
        $actions = array();
        foreach ($actionsProfile as $itemAction) {
            array_push($actions, $itemAction['Description']);
        }
        
        foreach ($actionAjax as $item) {
            $data = strtolower('ajax' . $item);
            array_push($actions, $data);
        }

        $arrayAction = Links::model()->findProfile(ucfirst($controllers), $idProfile);
        $arrayDifferences = $actionStatus->diffActions(ucfirst($controllers) . 'Controller', '', $arrayAction);

        $session = new CHttpSession;
        $session->open();
        $session['differences'] = $arrayDifferences;
               
        if (count($actions) <= 0) {
            return array(
                array('deny',
                    'users' => array('*'),
                ),
            );
        } else {
            return array(
                array('allow',
                    'actions' => $actions,
                    'users' => array('@'),
                ),
                array('deny',
                    'users' => array('*'),
                ),
            );
        }
    }

    public function actionIndex() {
        $this->render('index');
    }

    public function actionElejirCandidato() {
        Yii::app()->clientScript->registerScriptFile(
            Yii::app()->baseUrl . '/js/votacion/votacion.js', CClientScript::POS_END
        );
        $currentElection = Votacion::model()->getCurrentElection();
        $idZone = Yii::app()->user->_idZone;
        switch (true):
            case $idZone < 8:
                VotacionController::allAburraZone($currentElection);
                break;
            default:
                VotacionController::individualZone($idZone, $currentElection);
                break;
        endswitch;
    }

    protected function allAburraZone($currentElection) {
        $zoneName = Votacion::model()->getAburraZones();
        $quantity = count($zoneName) - 1;
        $count = 0;
        foreach ($zoneName as $itemZone):
            if ($count == $quantity):
                $candidates[$count] = Votacion::model()->getCandidatesZone($itemZone['IdRegion'],'Blanco');
            else:
                $candidates[$count] = Votacion::model()->getCandidatesZone($itemZone['IdRegion'],'Nada');
            endif;
            $count++;
        endforeach;
        $this->render('_candidatosZona', array(
            'candidatos' => $candidates,
            'nombreZona' => $zoneName, 'eleccion' => $currentElection
        ));
    }

    protected function individualZone($zone, $currentElection) {
        $zoneName = Votacion::model()->getZoneName($zone);
        $count = 0;
        foreach ($zoneName as $itemZone):
            $candidates[$count] = Votacion::model()->getCandidatesZone($itemZone['IdRegion'],'Blanco');
            $count++;
        endforeach;
        $this->render('_candidatosZona', array(
            'candidatos' => $candidates,
            'nombreZona' => $zoneName, 'eleccion' => $currentElection
        ));
    }

    public function actionRegistrarVoto() {
        if(isset($_POST['candidate'])):
            //RECIBIR PARAMETROS POST
            $user = Yii::app()->user->_document;
            //VALIDAR ESTADO USUARIO
            $status = Votacion::model()->getStatus($user);
            //INDEPENDIENTE DE LA RESPUESTA SE TERMINA LA SESION DEL USUARIO
            Yii::app()->user->logout();
            if ($status == 0):
                VotacionController::saveVote($_POST['candidate'],$_POST['zone'],$user);
            else:
                //CANDIDATO CON VOTO REGISTRADO ANTERIORMENTE
                //VALIDAR VOTACION DE FORMA FISICA
                $this->render('_votoAnulado');
            endif;
        else:
            //SE DETECTO UN INTENTO DE FRAUDE POR LO CUAL DEBE INICIAR SESION NUEVAMENTE
            Yii::app()->user->logout();
            $this->redirect("index.php?r=site/votacion");
        endif;
    }

    protected function saveVote($candidate, $zone, $user) {
        $date = date('Y-m-d');
        $time = date('H:i:s');
        $model = new Votacion;
        $model->CodCandidato = $candidate;
        $model->CodRegion = $zone;
        $model->CodUsuario = $user;
        $model->Fecha = $date;
        $model->Hora = $time;
        if ($model->save()):
            //SE REGISTRO EL VOTO CORRECTAMENTE
            //CAMBIAR ESTADO DEL USUARIO A 1
            Votacion::model()->chageStatus($user, 1);
            //MOSTRAR MENSAJE DE VOTO REGISTRADO
            $this->render('_votoRegistrado');
        else:
            //OCURRIO UN ERROR CON LA INFORMACION
            $this->render('_informacionErronea');
        endif;
    }

    public function actionVotoFisico() {
        Yii::app()->clientScript->registerScriptFile(
                Yii::app()->baseUrl . '/js/votacion/votacion.js', CClientScript::POS_END
        );
        $currentElection = Votacion::model()->getCurrentElection();
        $this->render('_VotoFisico', array('eleccion'=>$currentElection));
    }

    public function actionAjaxUserInformation() {
        if (isset($_POST['document'])):
            $userInformation = Votacion::model()->getUserInformation($_POST['document']);
            $this->renderPartial('_tblUsuario', array('userInformation'=>$userInformation));
        endif;
    }

    public function actionAjaxUnableUser() {
        if (isset($_POST['user'])):
            Votacion::model()->chageStatus($_POST['user'], 2);
            $userInformation = Votacion::model()->getUserInformation($_POST['user']);
            $this->renderPartial('_tblUsuario', array('userInformation'=>$userInformation));
        endif;
    }

    public function actionReporteVotacion() {
        Yii::app()->clientScript->registerScriptFile(
                Yii::app()->baseUrl . '/js/votacion/votacion.js', CClientScript::POS_END
        );
        $currentElection = Votacion::model()->getCurrentElection();
        $regions = Votacion::model()->getAllRegions($currentElection['IdEleccion']);
        $this->render('_ReporteVotacion', array('eleccion'=>$currentElection,'zonas'=>$regions));
    }

    public function actionAjaxGenerateStatistical() {
        if (isset($_POST['zonas'])):
            $zonas = $_POST['zonas'];
            $count = 0;
            foreach ($zonas as $zona):
                $statistical[$count] = Votacion::model()->getCandidatesZone($zona);
                $count++;
            endforeach;
            $this->renderPartial('_Statistical', array('statistical' => $statistical));
        endif;
    }
}
